import { ref, computed, reactive } from 'vue'
import { defineStore } from 'pinia'

export const musicPlayerStore = defineStore('musicPlayer', () => {
  // 是否正在播放
  const isPlaying = ref(false)
  // 播放模式：顺序播放0、列表循环1、单曲循环2
  const loopMethod = ref(0)
  // 音量大小：默认 80%
  const volume = ref(80)
  // 播放列表相关
  const playList = reactive({
    drawerShow: false,
    musicList: []
  })

  // 当前播放歌曲信息
  const musicInfo = reactive({
    num: '',
    name: '',
    singer: '',
    currentTime: '00:00',
    totalTime: '00:00',
    address: '',
    origin: {}
  })
  const times = reactive({
    currentTime: 0,
    totalTime: 0,
    progress: 0
  })
  const music = ref('')

  // 写入歌曲信息
  const SetMusicInfo = (e) => {
    music.value = new Audio(e.address)
    music.value.oncanplay = () => {
      musicInfo.name = e.name
      musicInfo.singer = e.singer
      musicInfo.totalTime = FormatTime(music.value.duration)
      times.totalTime = music.value.duration
      musicInfo.address = e.address
      musicInfo.origin = e
    }
    updateList(e)
    music.value.ontimeupdate = () => {
      times.currentTime = music.value.currentTime
      musicInfo.currentTime = FormatTime(times.currentTime)
      times.progress = parseFloat(((times.currentTime / times.totalTime) * 360).toFixed(1))
      // console.log(times.progress)
      music.value.volume = parseFloat(volume.value / 100)
      loopMethod.value == 2 ? (music.value.loop = true) : (music.value.loop = false)
    }
    music.value.onended = () => {
      let index = playList.musicList.indexOf(e)
      if (index != playList.musicList.length - 1) {
        Play(playList.musicList[index + 1])
      } else {
        if (loopMethod.value == 1) {
          Play(playList.musicList[0])
        }
      }
    }
  }

  // 播放列表更新事件
  const updateList = (e) => {
    if (playList.musicList.length == 0) {
      playList.musicList.push(e)
    } else {
      if (
        playList.musicList.every(function (item, index, array) {
          return e.address != item.address
        })
      ) {
        playList.musicList.push(e)
      }
    }
    console.log(playList.musicList)
  }

  // 拖拽进度条事件
  const SetPorgress = (value) => {
    // console.log(value)
    times.progress = value
    times.currentTime = parseInt((value / 360) * times.totalTime)
    if (music.value != '') {
      music.value.currentTime = times.currentTime
    }
  }

  // 格式化歌曲时长
  const FormatTime = (e) => {
    let minute = parseInt(e / 60)
    let second = parseInt(e % 60)
    minute = minute < 10 ? '0' + minute : minute
    second = second < 10 ? '0' + second : second
    return minute + ':' + second
  }

  // 播放
  const Play = (target) => {
    if (music.value == '') {
      SetMusicInfo(target)
    } else {
      if (target == 'play') {
        // console.log(target)
      } else if (musicInfo.address != target.address) {
        Pause()
        SetMusicInfo(target)
      }
    }
    if (isPlaying.value == false) {
      music.value.play()
      isPlaying.value = true
    }
  }
  // 暂停
  const Pause = () => {
    if (isPlaying.value == true) {
      music.value.pause()
      isPlaying.value = false
    }
  }

  return {
    isPlaying,
    loopMethod,
    Play,
    Pause,
    musicInfo,
    times,
    music,
    SetPorgress,
    volume,
    playList
  }
})
